package com.iflytek.ssm.utils;

import java.sql.*;

public class DBUtil {

    /**
     * 驱动
     */
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    /**
     * URL：xxq-javase是数据库名称
     */
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/student_manager_system?useUnicode=true&characterEncoding=utf8";
    /**
     * 用户名
     */
    private static final String USERNAME = "root";

    /**
     * 密码
     */
    private static final String PASSWORD = "root";

    static {
        try {
            /**
             * 加载驱动
             */
            Class.forName(DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得一个链接
     *
     * @return
     * @throws SQLException
     */
    public static final Connection getConnection() throws SQLException {
        Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        // 如果不写，会默认为自动提交
        con.setAutoCommit(false);
        return con;
    }

    // 修饰符 返回类型 方法名(参数列表){
    // 方法体
    // }


    public static final void close(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }
    }

    public static final void close(PreparedStatement ps) throws SQLException {
        if (ps != null) {
            ps.close();
        }
    }

    public static final void close(Connection con) throws SQLException {
        if (con != null) {
            con.close();
        }
    }

}
